package cn.springcloud.fix.samples.controller;

import cn.springcloud.fix.common.core.base.Result;
import cn.springcloud.fix.samples.config.CommonProperties;
import cn.springcloud.fix.samples.service.UserService;
import cn.springcloud.fix.samples.vo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/test")
public class RedisTestController {

    @Autowired
    private CommonProperties commonProperties;

    @Autowired
    private UserService userService;

    @RequestMapping("/add")
    public String add() {
        User user = userService.save(new User(4, "李现", 30));
        log.info("添加的用户信息：{}", user.toString());
        return "success";
    }

    @RequestMapping("/add2")
    public String add2() {
        User user = userService.save2(new User(5, "李现2", 30));
        log.info("添加的用户信息：{}", user.toString());
        return "success";
    }

    @RequestMapping("/add3")
    public String add3() {
        User user = userService.save3(new User(6, "李现3", 30), new User(7, "李现7", 30));
        log.info("添加的用户信息：{}", user.toString());
        return "success";
    }

    @RequestMapping("/delete")
    public String delete() {
        userService.delete(4);
        return "success";
    }

    @RequestMapping("/delete2")
    public String delete2() {
        userService.delete2(4);
        return "success";
    }

    @CacheEvict(value = "user:a", allEntries = true)
    @RequestMapping("/delete3")
    public String delete3() {
        return "success";
    }

    @RequestMapping("/get/{id}")
    public User get(@PathVariable("id") String idStr) throws Exception {
        Integer id = Integer.parseInt(idStr);
        User user = userService.get(id);
        log.info("获取的用户信息：{}", user.toString());
        return user;
    }

    @RequestMapping("/throwEx")
    public Result throwEx() {
        int i = 0;
        int y = 10 / i;
        return Result.SUCCESS();
    }
}
